<?php

namespace MemLib;

/**
 * PublicFunc short summary.
 *
 * 常用的公共函数
 *
 * @version 1.0
 * @author 张云江
 */
class PublicFunc
{
    /**
     *yt 打印测试
     */
    public static function _print($data, $type = 'COM_PRINT')
    {
        if (!$GLOBALS['is_debug']) {
            return;
        }
        if (is_array($data) || is_object($data)) {
            echo "<pre>";
            if ($type == 'COM_PRINT') {
                print_r($data);
            } else {
                var_dump($data);
            }
            echo "</pre>";
        } else if ($data !== null) {
            if ($type == 'COM_PRINT') {
                echo $data;
            } else {
                var_dump($data);
            }
            echo "<br>";
        } else {
            echo "======测试断点======<br>";
        }
    }
    /**
     * 转义字符串
     * @param mixed $string 要转义的字符串
     * @param mixed $flag ENT_宏
     * @return string
     */
    public static function convertToHtml($string, $flag = ENT_QUOTES)
    {
        $string = trim($string);
        return htmlspecialchars($string, $flag);
    }
    /**
     * 讲转义后的字符转换成正常字符
     * @param mixed $string 转义过的字符串
     * @param mixed $flag ENT_宏
     * @return string
     */
    public static function convertToHtml_Dec($string, $flag = ENT_QUOTES)
    {
        return htmlspecialchars_decode($string, $flag);
    }

    /**
     * 批量将转义后的字符转换成正常字符
     * @param array $arr 目标数据（数组或字符串）
     * @param int $num 字符串反转次数
     * @return string|array
     */
    public static function convertToHtml_Batch_Dec(&$arr, $num = 2)
    {
        if (is_array($arr)) {
            foreach ($arr as $key => &$value) {
                if (is_array($value)) {
                    self::convertToHtml_Batch_Dec($value);
                } else {
                    for ($i = 0; $i < $num; $i++) {
                        $arr[$key] = self::convertToHtml_Dec($arr[$key]);
                    }
                }
            }
        } else if (!empty($arr)) {
            for ($i = 0; $i < $num; $i++) {
                $arr = self::convertToHtml_Dec($arr);
            }
        }
        return $arr;
    }

    /**
     * 转换时间(安全转换)
     * @param mixed $date 要转换的时间
     * @param string $format 格式化字符串
     * @return string
     */
    public static function convertToDate($date, $format = "Y-m-d H:i:s")
    {
        $time = strtotime($date);
        $time1 = strtotime("");
        if ($time == $time1)
            return "0000-00-00 00:00:00";
        return date($format, strtotime($date));
    }

    /**
     * 获取星期描述
     * @param mixed $date 日期
     * @param mixed $prefix 星期前缀
     * @return string
     */
    public static function getWeek($date, $prefix = "")
    {
        $weekarray = array("{$prefix}日", "{$prefix}一", "{$prefix}二", "{$prefix}三", "{$prefix}四", "{$prefix}五", "{$prefix}六");
        return $weekarray[date("w", strtotime($date))];
    }

    /**
     * 转义数据库更新数组
     * @param $data
     */
    public static function convertToHtmlKeyVal($data)
    {
        $newdata = array();
        foreach ($data as $key => $val) {
            $key = self::convertToHtml($key);
            $val = self::convertToHtml($val);

            $newdata[$key] = $val;
        }
        return $newdata;
    }
    /**
     * 苹果验证接口
     * @param $data
     * @return object
     */
    public static function sendApple($receipt_data, $password, $sandboxStatus = 0)
    {
        $sandboxCurl = "https://sandbox.itunes.apple.com/verifyReceipt"; //测试路径
        $formalityCurl = "https://buy.itunes.apple.com/verifyReceipt"; //正式开发路径

        if ($sandboxStatus == 1) {
            $url = $sandboxCurl;
        } else {
            $url = $formalityCurl;
        }
        $JSON = '{"receipt-data":"' . $receipt_data . '","password":"' . $password . '"}';

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1); //一定要post形式传参
        curl_setopt($ch, CURLOPT_POSTFIELDS, $JSON);
        $result = curl_exec($ch);
        curl_close($ch);
        \Mlog::info('苹果订阅返回数据 = ' . json_encode($result, JSON_UNESCAPED_UNICODE));
        return json_decode($result, true);
    }
}
